using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Avalonia.Controls;
using Avalonia.Media;
using AvaloniaApplication1.Factory;
using AvaloniaApplication1.Models;
using AvaloniaApplication1.Service;
using AvaloniaApplication1.ViewModels.CardViewModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using SukiUI;
using SukiUI.Controls;
using SukiUI.Dialogs;

namespace AvaloniaApplication1.ViewModels;


public class MainWindowViewModel : INotifyPropertyChanged
{

    private SukiSideMenuItem _selectedMenuItem;

    /// <summary>
    /// 菜单项集合
    /// </summary>
    public ObservableCollection<SukiSideMenuItem> MenuItems { get; set; }
    
    
    /// <summary>
    /// 主题服务
    /// </summary>
    public  ThemeService _ThemeService;


    private readonly IViewModelFactory<DeviceCardModel, DeviceCardViewModel> _deviceCardVmFactory;

    /// <summary>
    /// 选中的菜单项
    /// </summary>
    public SukiSideMenuItem SelectedMenuItem
    {
        get => _selectedMenuItem; //外部访问SelectedMenuItem属性时，返回_selectedMenuItem的值
        set
        {
            _selectedMenuItem = value; //外部给SelectedMenuItem赋值时，将_selectedMenuItem的值设置为value
            OnPropertyChanged(); //实际上等价于 OnPropertyChanged("SelectedMenuItem") 这个会自动触发 PropertyChanged 事件
        }
    }
    /// <summary>
    /// 属性改变事件
    /// </summary>
    public event PropertyChangedEventHandler? PropertyChanged;

    /// <summary>
    /// 属性改变
    /// </summary>
    /// <param name="name">属性名</param>
    private void OnPropertyChanged([CallerMemberName] string? name = null)
    {
        //  触发属性改变事件 自动更新当前选中的对象（菜单项）的各种属性，比如菜单项的图标、标题、pageContent
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

    public MainWindowViewModel(ThemeService themeService,
        SettingsPageViewModel settingsPageViewModel,
        HomePageViewModel homePageViewModel ,
        DevicePageViewModel devicePageViewModel
        )
    {
        _ThemeService=themeService;


        MenuItems = new ObservableCollection<SukiSideMenuItem>
        {
            new()
            {
                Header = "首页",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M505.5488 23.9104l-447.5904 376.32a85.0432 85.0432 0 0 0 54.784 150.1696h76.8l3.7376 0.256c4.096 0.512 9.0112 6.0416 9.0112 12.544v326.144a122.2144 122.2144 0 0 0 122.0096 121.856h95.5904A89.6 89.6 0 0 0 509.4912 921.6v-179.2c0-31.3856 17.92-53.76 35.328-53.76h36.864c17.408 0 35.328 22.3744 35.328 53.76V921.6c0 49.4592 40.0896 89.6 89.6 89.6h95.5392a121.9584 121.9584 0 0 0 122.0608-121.856V563.2a12.8 12.8 0 0 1 12.8-12.8h76.8a85.0432 85.0432 0 0 0 54.6816-150.1696L620.8512 23.9104a89.6 89.6 0 0 0-115.3024 0z m65.8944 58.7776l447.6416 376.32a8.2432 8.2432 0 0 1-5.2736 14.592h-76.8A89.6 89.6 0 0 0 847.36 563.2v326.144c0 24.832-20.2752 45.056-45.2608 45.056H706.56a12.8 12.8 0 0 1-12.8-12.8v-179.2c0-70.5024-48.128-130.56-112.128-130.56h-36.864c-64 0-112.128 60.0576-112.128 130.56V921.6a12.8 12.8 0 0 1-12.8 12.8H324.352a45.3632 45.3632 0 0 1-45.2608-45.2608V563.2A89.6 89.6 0 0 0 199.936 474.112l-8.192-0.512L112.64 473.6a8.2432 8.2432 0 0 1-5.3248-14.592l447.5904-376.32a12.8 12.8 0 0 1 16.4864 0z"),
                    Width = 24,
                    Height = 24,
                },

                PageContent = homePageViewModel
            },
            new()
            {
                Header = "设置",
                Icon = new PathIcon
                {
                    Data = Geometry.Parse(
                        "M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2.5L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z")
                },
                PageContent = settingsPageViewModel
            },
            new()
            {
            Header = "设备列表",
            Icon = new PathIcon
            {
                Data = Geometry.Parse("M12,20.5L23.19,12.5L12,5.5L0.81,12.5L12,20.5Z")
            },
            PageContent = devicePageViewModel
        }
        };

        SelectedMenuItem = MenuItems.FirstOrDefault();
    }
    private T SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return field;
        field = value;
        OnPropertyChanged(propertyName);
        return value;
    }


    /// <summary>
    /// 主题切换
    /// </summary>
    public void ChangeTheme()
    {
        SukiTheme.GetInstance().SwitchBaseTheme();
        IsDarkTheme = !IsDarkTheme;

        #region 学习如何全局共享状态
        _ThemeService.IsDarkTheme=IsDarkTheme;
        #endregion

        // 切换主题后，发送主题变更消息
        WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(IsDarkTheme));
    }
    private bool _isDarkTheme =true;

    private bool IsDarkTheme
    {
        get => _isDarkTheme;
        set => SetProperty(ref _isDarkTheme, value);
    }




}